package csel.model;

public class Projectile extends MobileObject implements Ticked
{
	private static final long serialVersionUID = 5L;

	private Impactor myImpactor;
	private Direction myDirection;
	private GameGlobals gameGlobals;
	private int speed;
	boolean init = false;

	public static Projectile makeProjectile(GameGlobals g, Tile t, Direction d,
			Impactor i, int speed, String typeId)
	{
		Projectile nP = new Projectile(g, t, d, i, speed, typeId);
		if(t.canEnter(nP, null))
		{
			i.attach(nP);
			nP.gameGlobals.getChronoTrigger().addTicked(nP);
			nP.move(t);
			nP.move(d);
			System.out.println();
			return (nP);
		}
		return null;
	}

	private Projectile(GameGlobals g, Tile t, Direction d, Impactor i, int speed, String typeId)
	{
		// TODO fix me
		super(typeId);
		this.myImpactor = i;
		this.myDirection = d;
		this.gameGlobals = g;
		this.speed = speed;
	}

	public int getSpeed()
	{
		return (speed);
	}

	@Override
	public boolean isImpassable()
	{
		// TODO Does this make sense?
		return false;
	}

	@Override
	protected final void enterTile(Tile tile)
	{
		tile.enter(this);
		myImpactor.impact(tile);
		// TODO call impact() here?
	}

	@Override
	protected void leaveTile()
	{
		getTile().leave(this);
	}
	@Override
	public boolean can(MoveAbilities ability)
	{
		if((ability == MoveAbilities.Pierce)||(ability == MoveAbilities.Float))
		{
			return(true);
		}
		leaveTile();
		return(false);
	}
	public synchronized boolean move(Tile dest)
	{
		boolean result = super.move(dest);
		
		if(!dest.canEnter(this, null))
		{
			myImpactor.impact(dest);
			gameGlobals.getChronoTrigger().removeTicked(this);
			leaveTile();
		}
		
		return(result);
	}
}
